package org.dromara.common.core.context;


import cn.hutool.core.lang.Dict;

public class ContextHolder {
    private static ThreadLocal<Dict> contexts = new InheritableThreadLocal<Dict>();

    public static String LOGIN_USER_KEY = "__user_key_";

    public static String TOKEN_KEY = "__token_";


    public static String APP_CODE_KEY = "appCode";

    public static void add(Dict context) {
        contexts.set(context);
    }

    public static void clear() {
        contexts.set(null);
    }

    public static Dict getCurrentContext() {
        Dict context = contexts.get();
        if (context == null) {
            context = new Dict();
            add(context);
        }
        return context;
    }

    public static String getAppCode() {
        return getCurrentContext().getStr(APP_CODE_KEY);
    }

    public static void setAppCode(String appCode) {
        getCurrentContext().put(APP_CODE_KEY, appCode);
    }

    public static Long getUserId() {
        return getCurrentContext().getLong(LOGIN_USER_KEY);
    }

    public static void setUserId(Long userId) {
        getCurrentContext().put(LOGIN_USER_KEY, userId);
    }

    public static void setToken(String token) {
        getCurrentContext().put(TOKEN_KEY, token);
    }

    public static String getToken() {
        return getCurrentContext().getStr(TOKEN_KEY);
    }

}
